package user

import (
	"gitlab.local/TerraMaster/tos-modules/core/acl"
	//database "gitlab.local/DO-module/new-filemanage-module/src/core/sqlitedb"
	"gitlab.local/TerraMaster/tos-modules/database"
	"gitlab.local/TerraMaster/tos-modules/directory"
	"gitlab.local/golibrary/sysconfig"
	"strings"
)

type UserSlice []UserInfo

type UserInfo struct {
	Uid          string   `json:"uid"`
	Name         string   `json:"name"`
	Email        string   `json:"email"`
	Phone        string   `json:"phone"`
	Description  string   `json:"description"`
	QuotaSize    bool     `json:"quota_size"`
	CloudActive  int      `json:"cloud_active"`
	GroupList    []string `json:"group_list"`
	Alias        string   `json:"alias"`
	IsFirstAdmin bool     `json:"is_first_admin"`
	IsNvr        bool     `json:"is_nvr"`
	State        int      `json:"state"`
	Disabled     bool     `json:"disabled"`
}

func (us UserSlice) Len() int {
	return len(us)
}
func (us UserSlice) Swap(i, j int) {
	us[i], us[j] = us[j], us[i]
}
func (us UserSlice) Less(i, j int) bool {
	return strings.Compare(us[i].Name, us[j].Name) < 0
}

type Group struct {
	Gid         string   `json:"gid"`
	Name        string   `json:"name"`
	Description string   `json:"description"`
	IsNvr       bool     `json:"is_nvr"`
	UserList    []string `json:"user_list"`
	Disabled    bool     `json:"disabled"`
}

type GroupSlice []Group

func (us GroupSlice) Len() int {
	return len(us)
}
func (us GroupSlice) Swap(i, j int) {
	us[i], us[j] = us[j], us[i]
}
func (us GroupSlice) Less(i, j int) bool {
	return strings.Compare(us[i].Name, us[j].Name) < 0
}

type UserService interface {
	UserList(start, limit int) (result []UserInfo, err error)
	SysUserList(start, limit int) (result []UserInfo, err error)
	GetUsersByGroup(group string) []string
	GetUserInGroups(username string) []string
	GetUserOrGroup() (map[string][]string, error)

	GroupService
}

type GroupService interface {
	GroupList(int, int) ([]Group, error)
}

type User struct {
	liteDb        database.DbGetter
	configService sysconfig.Setter
	dirService    directory.FolderService
	aclGetter     acl.FolderAttrService
}

func New(dbGetter database.DbGetter,
	configService sysconfig.Setter,
	dirService directory.FolderService,
	aclGetter acl.FolderAttrService,
) *User {
	user := &User{
		liteDb:        dbGetter,
		configService: configService,
		dirService:    dirService,
		aclGetter:     aclGetter,
	}
	return user
}
